var Animation = require('./animation');

function FrictionAnimation(b, v0) {
  Animation.call(this);
  this.name = 'friction';
  this.value = b;
  this.v0 = v0;
  this.v = v0;
  if (this.v0 > 0) {
    this.a = - 0.006;
  } else {
    this.a = 0.006;
  }
  this.totalTime = 5000;
}

FrictionAnimation.prototype = new Animation();

FrictionAnimation.prototype.step = function() {
  var lastTime = this.lastTime || this.startTime;
  var deltaTime = Date.now() - lastTime;
  this.lastTime = Date.now();
  var v = Math.abs(this.v) * this.a * deltaTime + this.v;
  if (v * this.v0 <= 0 || Math.abs(v) <= 0.01) {
    this.stop();
    return;
  }
  this.v = v;
  this.value += this.v * deltaTime;
  this.emit('step', this.value);
};

module.exports = FrictionAnimation;